<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use EasyWeChat;
use App\Services\RedisService;
use App\Models\Gift;
use App\Models\LotteryResult;


class IndexController extends Controller
{
    

    public function  index(Request $request)
    {
        $wxuser =  session('wechat.oauth_user.default'); // 公众号
        $openid = $wxuser->id;
        $user = $app->user->get($openid);
    }

    public function draw(Request $request)
    {
    	$rs['status'] = false;
        $wxuser =  session('wechat.oauth_user.default'); // 公众号
        //dd($wxuser);
        $openid = '23232323223423234234';
        $wl = RedisService::get_whitelist($openid);

        if(!$wl) {
            
            // 判断今天还有没有抽奖机会
            $key = 'h5:drawtimes:'.date('Ymd').':'.$openid;
            $times = RedisService::get($key);

            if(!isset($times)) {
                $timestamp = strtotime("+1 day");
                $dts = config('app.draw_times');
                RedisService::set($key,$dts,$timestamp);
               
                } elseif($times<1) {
                $rs['msg'] = '已经没有抽奖机会了';
                return response()->json($rs);   
            }    
        }




        
        $maxKey = 'h5:gift:4';
        $max = RedisService::get($maxKey);
        
        $maxData = json_decode($max);
        $num  = mt_rand(1,$maxData->num);       
        $gift = $this->getGift($num);
        $data = json_decode($gift);
        //dd($data);
        if($data) {
            

            $info['openid']       = $openid;
            $info['nickname']     = array_get($wxuser,'nickname');
            $info['headimgurl']   = array_get($wxuser,'avatar');
            $info['gift_id']      = $data->id;
            $info['gift_name']    = $data->name;
            $info['gift_content'] = $data->content;
            $info['gift_type']    = object_get($data,'type',1);
            $info['code'] = date('YmdHis').mt_rand(10000,99999);
            $result = LotteryResult::create($info);         
            $key = 'h5:drawtimes:'.date('Ymd').':'.$openid;
            RedisService::decr($key);
            $key = 'h5:gift:num:'.$data->id;            
            RedisService::decr($key);

            // 中奖数+1
            RedisService::set_gifted_num('total');  
            RedisService::set_gifted_num(date('Ymd'));
            $rs['status'] = true;
            $data->result_id = $result->id;
            $rs['data'] = $data;
            
            return response()->json($rs);
        }
        return response()->json($rs);
    }

    public function saveInfo(Request $request)
    {
    	$rs['status'] = false;
       // $wxuser =  session('wechat.oauth_user.default'); // 公众号
    	if(empty($request->gift_code)) {
    		$rs['msg'] = '奖品code错误';
    		return response()->json($rs);
    	}



    	$key = 'h5:gift:'.$request->gift_id;
    	$info = RedisService::get($key);
    	$info = json_decode($info);
    	//dd($info);
    	if($info->code != $request->gift_code) {
    		$rs['msg'] = '奖品code错误';
    		return response()->json($rs);
    	}

    	$openid = '232323232332';
    	$where['openid']  = $openid;
    	$where['gift_id'] = $request->gift_id;
    	$result = LotteryResult::where($where)->first(); 
    	if(!$result) {
    		$rs['msg'] = '信息错误';
    		return response()->json($rs);
    	}
    	$result->truename = $request->truename;
    	$result->mobile = $request->mobile;
    	$result->address = $request->address;
    	$flag = $result->save();   	
    	if($flag) {
    		
    		$rs['status'] = true;
    		$rs['msg'] = '操作成功';
    		return response()->json($rs);
    	}
    	return response()->json($rs);
    }

   

    private function getGift($num)
    {
        $key = 'h5:gift:';
        $numkey = 'h5:gift:num:';
        $gift = '';
        $gift_num = 0;

        // 一共19个实物奖品
        // 一等奖
        if($num<4) {
            $key = $key.'1';
            $numkey = $numkey.'1';
            $gift = RedisService::get($key);
            $gift_num = RedisService::get($numkey);
        } else if($num>3&&$num<9) {
            $key = $key.'2';
            $numkey = $numkey.'2';
            $gift = RedisService::get($key);
            $gift_num = RedisService::get($numkey);
        } else if($num>8&&$num<19) {
            $key = $key.'3';
            $numkey = $numkey.'3';
            $gift = RedisService::get($key);
            $gift_num = RedisService::get($numkey);
        } elseif($num>18&&$num<69){
            $key = $key.'9';
            $numkey = $numkey.'9';
            $gift = RedisService::get($key);
            $gift_num = RedisService::get($numkey);
        }else if($num>20068) {
            // 京东券
            $key = $key.'4';
            $numkey = $numkey.'4';
            $gift = RedisService::get($key);
            $gift_num = RedisService::get($numkey);
        } else {
            $rand = mt_rand(7,8);
            $key = $key.$rand;
            $numkey = $numkey.$rand;
            $gift = RedisService::get($key);
            $gift_num = RedisService::get($numkey);
        }
        //dd($key,$num);
        if($gift_num<1) {
            $gift = RedisService::get('h5:gift:4');
            
        }
        //dd($num);
        return $gift;
    }

    public function myGift(Request $request) 
    {
    	$rs['status'] = false;
        $wxuser =  session('wechat.oauth_user.default');
    	$where['openid'] = '232323232332';
    	$list = LotteryResult::where($where)->get();
    	if($list) {
    		$rs['data']= $list;
    	}
    	return response()->json($rs);
    }

    public function test(Request $request)
    {
        // $string = '2@#@-^$%^$%^&^**^&*df-k33';
        // $string = preg_replace('/[^a-zA-Z0-9_.\-]/', '', $string);
        // echo $string;

       
      
        $rs = RedisService::keys('h5:gift:');

        //echo count($rs);die;
        RedisService::set_gifted_num();
        RedisService::set_gifted_num(date('Ymd'));
    }

    public function times()
    {
        return response()->json(['status'=>true,'times'=>config('app.draw_times')]);
    }

    public function gitPull()
    {
        echo shell_exec('whoami');
        echo shell_exec("sudo /usr/bin/git pull 2>&1");
    }
}
